package flc.ast.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.contrarywind.adapter.WheelAdapter;
import com.contrarywind.view.WheelView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.adapter.BeatAdapter;
import flc.ast.databinding.DialogBeatBinding;
import stark.common.basic.base.BaseSmartDialog;

public class BeatDialog extends BaseSmartDialog<DialogBeatBinding> implements View.OnClickListener {

    private int currentBeats;
    private List<Integer> beatList;
    private BeatAdapter beatAdapter;

    public BeatDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    public void setBeat(int beats) {
        currentBeats = beats;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_beat;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvBeatCancel.setOnClickListener(this);
        mDataBinding.tvBeatConfirm.setOnClickListener(this);

        beatList = new ArrayList<>();
        for (int i = 1; i <= 16; i++) {
            beatList.add(i);
        }

        int startBeatPos = 0;
        for (int i = 0; i < beatList.size(); i++) {
            if (beatList.get(i) == currentBeats) {
                startBeatPos = i;
                break;
            }
        }

        mDataBinding.rvBeatList.setLayoutManager(new GridLayoutManager(getContext(), 6));
        beatAdapter = new BeatAdapter();
        mDataBinding.rvBeatList.setAdapter(beatAdapter);
        beatAdapter.setSelPosition(startBeatPos);
        beatAdapter.setList(beatList);
        beatAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                beatAdapter.setSelPosition(position);
                beatAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBeatCancel:
                dismiss();
                break;
            case R.id.tvBeatConfirm:
                dismiss();
                if (listener != null) {
                    listener.onClickConfirm(beatList.get(beatAdapter.getSelPosition()));
                }
                break;
        }
    }

    private IListener listener;

    public void setListener(IListener listener) {
        this.listener = listener;
    }

    public interface IListener {
        void onClickConfirm(int note);
    }
}
